<?php 
class LoginApp extends CommonApp {
   
   	//登陆页面
    public function indexAct(){
    	if($_POST){
            $password = MD5(MD5($_POST['password']));
            $tel = $_POST['tel'];
            if(!$tel) $this->ajaxReturn(300,'手机号码不能为空！');
            if(!$password) $this->ajaxReturn(300,'密码不能为空！');
            $result = DB('members')->field('id,tel,type')->where("tel='{$tel}' AND password='{$password}' AND publish = 1")->cache()->find();
            if(!$result)
                $this->ajaxReturn(300,'号码或者密码输入错误!');
            $_SESSION['member'] = $result;
            $this->ajaxReturn(200,'登陆成功！');
        }else{
            if(isset($_SESSION['tel']))
                $this->assign('tel',$_SESSION['tel']);
            $this->display();
        }
    }

    //注册页面及短信服务
    public function registerAct(){
    	if($_POST){
            if(!My::is_tel($_POST['tel']))
                $this->ajaxReturn(300,'手机号码填11写错误!');
            $Sendsms = new Sendsms();
            $result = $Sendsms->sms($_POST['tel']);
            // $result['result'] = 'OK';
            // $result['code'] = '123';
            if($result['result'] == 'OK') $this->ajaxReturn(200,$result['code']);
        }else $this->display();
    }

    //验证登陆
    public function verifyAct(){
        if($_POST){
            $tel = $_POST['tel'];
            $password = $_POST['password'];
            $password2 = $_POST['password2'];
            if(!My::is_tel($tel))
                $this->ajaxReturn(300,'手机号码填写错误!');
            elseif(!My::is_string($password))
                $this->ajaxReturn(300,'密码填写错误!');
            elseif(strlen($password)<6)
                $this->ajaxReturn(300,'密码不能小于6位!');
            elseif(!My::is_string($password2))
                $this->ajaxReturn(300,'确认密码填写错误!');
            elseif($password !== $password2)
                $this->ajaxReturn(300,'两次密码填写不一致!');
            elseif(DB('members')->where("tel='{$tel}'")->cache()->count()>0)
                $this->ajaxReturn(300,'该手机号码已存在,不能重复注册!');
            else{
                $data['tel'] = $tel;
                $data['password'] = MD5(MD5($password));
                $data['adddate'] = date('Y-m-d H:i:s');
                $data['type'] = 1;
                $data['publish'] = 1;
                $ok = DB('members')->clear()->add($data);
                if($ok>0){
                    $_SESSION['tel'] = $tel;
                    $this->ajaxReturn(200,'注册成功！');
                }else $this->ajaxReturn(300,'注册失败!db错误!');
            }
        }
    }

    public function getpasswordAct(){
        if($_POST){
            $tel = $_POST['tel'];
            $password = $_POST['password'];
            $password2 = $_POST['password2'];
            if(!My::is_tel($tel))
                $this->ajaxReturn(300,'手机号码填写错误!');
            elseif(!My::is_string($password))
                $this->ajaxReturn(300,'新密码填写错误!');
            elseif(strlen($password)<6)
                $this->ajaxReturn(300,'密码不能小于6位!');
            elseif(!My::is_string($password2))
                $this->ajaxReturn(300,'确认新密码填写错误!');
            elseif($password !== $password2)
                $this->ajaxReturn(300,'两次密码填写不一致!');
            elseif(DB('members')->where("tel='{$tel}'")->cache()->count()==0)
                $this->ajaxReturn(300,'该手机号码不存在，请重新输入!');
            else{
                $data['password'] = MD5(MD5($password));
                $data['editdate'] = date('Y-m-d H:i:s');
                $ok = DB('members')->clear()->where("tel='{$tel}'")->save($data);
                if($ok>0){
                    $_SESSION['tel'] = $tel;
                    $this->ajaxReturn(200,'更改成功！');
                }else $this->ajaxReturn(300,'更改失败!db错误!');
            }
        }else $this->display();
    }

}